public with sharing class TSRResourceAjustmentController {

      public Activity_Information__c ActivityInfo{get;set;}
     public CommonAttribute CommonAtt{get;set;}
     public String OutletName{get;set;}
     public CommonMethod getCommonMethod;
     public CommonRecursionMethod getRecursionMethod;
     public List<String> ColumnETList{get;set;}
     public  List<DynamicColumnEntry> ETDynamicColumnEntryList{get;set;}
     public  List<DynamicColumnEntry> FGDynamicColumnEntryList{get;set;}
      
     public List<Activity_Competition_Brand_Detail__c> ETCompetitionList{get;set;}
     public List<String> ColumnFGList{get;set;}
      public Participator_Detail_Area__c parDetailArea;
      public List<SelectOption> ParticipatorKAList{get;set;}//参与列表中的KA
     List<Resource_Allocation_Detail__c> ETResourceDetailList;
     List<Resource_Allocation_Detail__c> FGResourceDetailList;
     public Boolean IsDisplayForSubmit{get;set;}
     public Boolean IsDisplayForSave{get;set;}
     public Boolean IsDisplayForComfirmDate{get;set;}
     public string SubordinateUserId{get;set;}//获取当前用户下面的所有用户
     public List<id> SubordinateUserIdList{get;set;}//获取当前用户下面的所有用户
     public List<SelectOption> CityList{get;set;}//城市列表
     public Integer outletNumber{get;set;}
     public String ETorKA{get;set;}
     public String FGorOT{get;set;}
     public User user
     {
         get{return [select id,UserRegion__c from User where id=:UserInfo.getUserId()];}
     }
     public String RecordId
     {
           get
           {
               return String.valueOf(System.currentPageReference().getParameters().get('id')).substring(0,15);  
           }
           set;
     }
  
    
     
   public TSRResourceAjustmentController(ApexPages.StandardController controller)
     {
         getCommonMethod=new CommonMethod();
         CommonAtt=new CommonAttribute ();
         getRecursionMethod=new CommonRecursionMethod();
     
           
         if(CityList==NULL)
             CityList=new List<SelectOption>();
         else
             CityList.clear();
         
         if(ParticipatorKAList==NULL)
             ParticipatorKAList=new List<SelectOption>();
         else
             ParticipatorKAList.clear();
             
         ParticipatorKAList=getCommonMethod.ParticipatorKAList(RecordId,false);//取得用户ＫＡ列表
         CityList=getCommonMethod.ParticipatorCityList(RecordId,false);//取得用户城市列表
      
         IsDisplayForSave=false;
         IsDisplayForSubmit=false;
         IsDisplayForComfirmDate=false;
         
             ActivityInfo=[Select Id,Name,  Activity_End_Date__c, Activity_Notice__c, Activity_Stage__c, Activity_Start_Date__c, 
                               Activity_Type_State__c, Activity_Type__c,Serial_Number__c,Regional_Activity_Type__c 
                               FROM Activity_Information__c where id=: RecordId]; 
         
           //判断资源类型
               if(ActivityInfo.Activity_Type__c=='SSY'){
                    ETorKA='KA';
                    FGorOT='OT';
                 }else{
                    ETorKA='ET';
                    FGorOT='FG';
                 }
         //查出生效的审批记录
             List<Participator_Detail_Area__c> parDetailAreaList=[select id,Submit_ASM__c,Outlet_Amount__c,Approve_Type__c,Approve_Description__c 
                                                                                                    from Participator_Detail_Area__c where Activity_Information__c=:RecordId  and 
                                                                                                    Submit_ASM__c=:UserInfo.getUserId()  and IsActive__c=true limit 1];
             if(parDetailAreaList!=NULL && parDetailAreaList.size()>0)
                 parDetailArea=parDetailAreaList[0];  
             
           outletNumber=[select count() from Participator_Detail_Outlet__c where ActivityId__c=:RecordId];     
        
     if(ActivityInfo.Activity_Stage__c=='活动已取消')
             {
                  IsDisplayForSubmit=false;
                   IsDisplayForSave=false;
                   IsDisplayForComfirmDate=false;
             }  
             else
             {
                  Date StartDT=ActivityInfo.Activity_Start_Date__c;                   
                  if(Date.today()>= StartDT.addDays(-21))//开始活动前21天不可以调时间
                  {   
                       IsDisplayForSubmit=false;
                       IsDisplayForSave=false;
                       IsDisplayForComfirmDate=false;
                   }      
                   else
                   {                     
                        if(parDetailArea != NULL)
                        {
                         if(ActivityInfo.Activity_Type__c=='Region' || ActivityInfo.Activity_Type__c=='Region(剩余资源)'){
                            if(parDetailArea.Approve_Type__c=='SRTM已审批' && parDetailArea.Approve_Description__c=='同意')
                            {
                                IsDisplayForSubmit=true;
                                 IsDisplayForComfirmDate=true;
                                 IsDisplayForSave=true;
                             }
                             if( parDetailArea.Approve_Description__c=='拒绝')
                              {
                                  IsDisplayForSubmit=true;
                                 IsDisplayForComfirmDate=false;
                                 IsDisplayForSave=true;
                             }
                            }
                            
                            else{
                             if(parDetailArea.Approve_Type__c=='ASM已提交')
                                IsDisplayForSubmit=true;                         
                                IsDisplayForSave=true;          
                                if(ActivityInfo.Activity_Type__c=='KA')
                                      IsDisplayForComfirmDate=true;      
                            }
                            if(parDetailArea.Approve_Type__c=='ASM已确认时间')
                             {
                                IsDisplayForSubmit=false;
                                IsDisplayForComfirmDate=false;    
                                IsDisplayForSave=false;     
                             }
                        }
                        else
                        { 
                           if(outletNumber>0){
                              IsDisplayForSubmit=true;
                              IsDisplayForSave=true;
                            }
                              IsDisplayForComfirmDate=false;
                        }
                   }
           }   
             
          ETResourceDetailList=new List<Resource_Allocation_Detail__c>();
          FGResourceDetailList=new List<Resource_Allocation_Detail__c>();
              
      
          if(CommonAtt.CitySelectedValue==NULL && CityList.size()>0)
             CommonAtt.CitySelectedValue=CommonAtt.AllCityList[0].getValue();
          if(CommonAtt.KASelectedValue==NULL && ParticipatorKAList.size()>0)
              CommonAtt.KASelectedValue=ParticipatorKAList[0].getValue();
              
         LoadParticipatorOutletList();//加载ＫＡ门店列表
         LoadCompetitionList();//加载KA竞品信息
     }
   
     public PageReference OutletQuery()
     {
         LoadParticipatorOutletList();//加载ＫＡ门店列表
        LoadCompetitionList();//加载ET,FG竞品信息
         return null;
     }
     public void LoadParticipatorOutletList()
     {
         String filter=GetSqlFilter(); 
         ETresourceDetailList.clear();
         FGresourceDetailList.clear();

         List<Resource_Allocation_Detail__c> resourceDetailList=new List<Resource_Allocation_Detail__c>();
         resourceDetailList=getCommonMethod.LoadOutletList(filter,RecordId);//加载ET门店列表
         for(Resource_Allocation_Detail__c rad:resourceDetailList){
           if(rad.ReourceOutletType__c=='ET' || rad.ReourceOutletType__c=='KA')
               ETresourceDetailList.add(rad);
           else if(rad.ReourceOutletType__c=='FG' || rad.ReourceOutletType__c=='OT')
              FGresourceDetailList.add(rad);
         }
         ColumnETList=getCommonMethod.GetDynamicETColumnList(ETresourceDetailList);//获取ET列名
         ETDynamicColumnEntryList=getCommonMethod.GetDynamicEntryList(ETresourceDetailList);//获取ET门店列表
         if(ActivityInfo.Activity_Type__c!='Region(剩余资源)'){
              ColumnFGList=getCommonMethod.GetDynamicETColumnList(FGresourceDetailList);//获取FG列名
              FGDynamicColumnEntryList=getCommonMethod.GetDynamicEntryList(FGresourceDetailList);//获取FG门店列表  
           }         
     }

  
     public void LoadCompetitionList()
     {
      String filter=GetSqlFilter();
         String sql='select OutletName__c,SubmitASM__c,OutletId__c,Promotion_Type__c,Cost_Type__c,Competition_Cost_Source__c,'+
                            ' Money__c,Start_Date__c,End_Date__c,Display_Type__c from Activity_Competition_Brand_Detail__c'+
                            '  where ActivityId__c=\''+RecordId +'\''
                            +filter;
              //             throw new CustomException (sql);  
             ETCompetitionList=Database.Query(sql);
         
     }
     
     public String GetSqlFilter()
     {
          if(CommonAtt.CitySelectedValue==NULL)
             CommonAtt.CitySelectedValue=CommonAtt.AllCityList[0].getValue();
          if(CommonAtt.KASelectedValue==NULL)
              CommonAtt.KASelectedValue=CommonAtt.OutletKAList[0].getValue();
        String filter=' and Participator_Detail_Outlet__r.OwnerId=\''+user.id+'\' and Participator_Detail_Outlet__r.City__c=\''+CommonAtt.CitySelectedValue+'\'';
        if(OutletName!=NULL)
            filter+=' and Participator_Detail_Outlet__r.Outlet_Name__r.Name like \'%'+OutletName+'%\'';
        if(ActivityInfo.Activity_Type__c=='KA'){
            if(CommonAtt.OutletTypeSelectedValue!='' && CommonAtt.OutletTypeSelectedValue!=NULL)
                 filter+=' and Participator_Detail_Outlet__r.OutletType__c=\''+CommonAtt.OutletTypeSelectedValue+'\'';
            if(CommonAtt.ChannelSelectedValue!='' && CommonAtt.ChannelSelectedValue!=NULL)
                 filter+=' and Participator_Detail_Outlet__r.Channel__c=\''+CommonAtt.ChannelSelectedValue+'\'';
            filter+=' and Participator_Detail_Outlet__r.KA__c=\''+CommonAtt.KASelectedValue+'\'';
        }
                   
         return filter;
     }
    
 
}